package com.xxadmin.usercenter.controller;

import com.xxadmin.common.dto.PageInfoDTO;
import com.xxadmin.common.util.clone.CopyUtils;
import com.xxadmin.common.util.page.PageUtils;
import com.xxadmin.common.validate.ValidGroup1;
import com.xxadmin.common.validate.ValidGroup2;
import com.xxadmin.common.vo.PageInfoVO;
import com.xxadmin.common.vo.ResponseModel;
import com.xxadmin.common.vo.ResponseModelBuilder;
import com.xxadmin.common.web.BaseController;
import com.xxadmin.usercenter.dto.MenuInfoDTO;
import com.xxadmin.usercenter.dto.MenuInfoQueryDTO;
import com.xxadmin.usercenter.service.MenuService;
import com.xxadmin.usercenter.vo.request.MenuInfoQueryVO;
import com.xxadmin.usercenter.vo.request.MenuInfoVO;
import com.xxadmin.usercenter.vo.response.MenuInfoResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单
 *
 * @author:chenming
 * @date:2018/9/3
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/add")
    public ResponseModel addMenuInfo(@RequestBody @Validated(ValidGroup1.class) MenuInfoVO menuInfoVO){

        MenuInfoDTO menuInfoDTO = CopyUtils.copyBean(menuInfoVO,MenuInfoDTO.class);

        menuService.addMenuInfo(menuInfoDTO);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }


    @PutMapping("/update")
    public ResponseModel updateMenuInfo(@RequestBody @Validated(ValidGroup2.class) MenuInfoVO menuInfoVO){
        MenuInfoDTO menuInfoDTO = CopyUtils.copyBean(menuInfoVO,MenuInfoDTO.class);

        menuService.updateMenuInfo(menuInfoDTO);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }


    @GetMapping("/get/{id}")
    public ResponseModel<MenuInfoResponseVO> getMenuInfo(@PathVariable("id") Integer id){
        MenuInfoDTO menuInfoDTO = menuService.getMenuInfo(id);

        return ResponseModelBuilder.genericSuccessResponseModal(CopyUtils.copyBean(menuInfoDTO,MenuInfoResponseVO.class));
    }

    @GetMapping("/list")
    public ResponseModel<PageInfoVO> listMenuInfo(MenuInfoQueryVO queryVO){
        MenuInfoQueryDTO queryDTO = CopyUtils.copyBean(queryVO,MenuInfoQueryDTO.class);

        PageInfoDTO<MenuInfoDTO> pageInfoDTO = menuService.listMenuInfo(queryDTO);

        PageInfoVO<MenuInfoResponseVO> pageInfoVO = PageUtils.getPageInfoVO(pageInfoDTO,MenuInfoResponseVO.class);

        return ResponseModelBuilder.genericSuccessResponseModal(pageInfoVO);
    }
}
